package com.senney.practice.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class MysqlDemo {

	public static void main(String[] args) throws Exception {
		Properties props = new Properties();
		props.load(MysqlDemo.class.getResourceAsStream("/config/dbconfig.properties"));
		Connection con = null;
		PreparedStatement preperadStatement = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName(props.getProperty("jdbc.mysql.driver"));
			con = DriverManager.getConnection(props.getProperty("jdbc.mysql.url"), props.getProperty("jdbc.mysql.user"),
					props.getProperty("jdbc.mysql.password"));
			preperadStatement = con.prepareStatement("insert into userinfo values(null,?,?)");
			preperadStatement.setString(1, "Jack");
			preperadStatement.setInt(2, 21);
			preperadStatement.execute();
			preperadStatement.setString(1, "Merry");
			preperadStatement.setInt(2, 18);
			preperadStatement.execute();
			stmt = con.createStatement();
			rs = stmt.executeQuery("select * from userinfo");
			while (rs.next()) {
				System.out.println(rs.getInt(1) + "-" + rs.getString(2) + "-" + rs.getInt(3));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null)
				rs.close();
			if (stmt != null)
				stmt.close();
			if (preperadStatement != null)
				preperadStatement.close();
			if (con != null)
				con.close();
		}

	}

}
